<template>

  <el-form ref="form" style="padding-top: 10px;" :model="form" label-width="100px">
    <div class="com-form-title">基础信息</div>
    <el-row>
      <el-col :span="8">
        <el-form-item label="发料编号" prop="issueNumber">
          <el-input v-model="form.issueNumber" :disabled="true" placeholder="自动生成" />
        </el-form-item>
      </el-col>
      <el-col :span="8">
        <el-form-item label="领用时间" prop="recvTime">
          <el-date-picker v-model="form.recvTime" style="width:100%" type="date" :disabled="disableForm" />
        </el-form-item>
      </el-col>
      <el-col :span="8">
        <el-form-item label="仓库名称" prop="storehouseName">
          <!-- <el-input v-model="form.storehouseName" :disabled="disableForm" /> -->
          <el-input v-model="form.storehouseName" :disabled="disableForm" />
        </el-form-item>
      </el-col>
      <el-col :span="8">
        <el-form-item label="期次" prop="period">
          <el-input v-model="form.period" :disabled="disableForm" />
        </el-form-item>
      </el-col>
      <el-col :span="8">
        <el-form-item label="发料人" prop="sender">
          <el-input v-model="form.sender" :disabled="disableForm" />
        </el-form-item>
      </el-col>
      <el-col :span="8">
        <el-form-item label="分包队伍负责人" prop="stHead">
          <el-input v-model="form.stHead" :disabled="disableForm" />
        </el-form-item>
      </el-col>
      <el-col :span="8">
        <el-form-item label="供应商编号" prop="supplierNumber">
          <!-- <el-input v-model="form.supplierNumber" :disabled="disableForm" /> -->
          <el-input v-model="form.supplierNumber" :disabled="disableForm" />
        </el-form-item>
      </el-col>
      <el-col :span="8">
        <el-form-item label="供应商名称" prop="supplierName">
          <el-input v-model="form.supplierName" :disabled="disableForm" />
        </el-form-item>
      </el-col>
      <el-col :span="8">
        <el-form-item label="描述" prop="description">
          <el-input v-model="form.description" :disabled="disableForm" />
        </el-form-item>
      </el-col>
    </el-row>

    <div style="padding-bottom: 10px;">
      <el-button type="primary" @click="showTargetSetListModal = true">引入材料</el-button>
      <el-button type="primary" @click="deleteDetail">删除材料</el-button>
    </div>
    <com-table
      v-model="selectRow"
      highlight-current-row
      :data="targetList"
      :columns="columns"
      :no-page="true"
    >
      <template v-slot:number="{row}">
        {{ row.number||row.code }}
      </template>
      <template v-slot:name="{row}">
        <el-input v-model="row.name" />
      </template>
      <template v-slot:specification="{row}">
        <el-input v-model="row.specification" />
      </template>
      <template v-slot:unit="{row}">
        <el-input v-model="row.unit" />
      </template>
      <template v-slot:count="{row}">
        <el-input v-model="row.count" />
      </template>
      <template v-slot:settleNode="{row}">
        <el-input v-model="row.settleNode" />
      </template>
      <template v-slot:description="{row}">
        <el-input v-model="row.description" />
      </template>
      <template v-slot:remark="{row}">
        <el-input v-model="row.remark" />
      </template>

    </com-table>
    <el-button style="margin-top:10px;" type="primary" @click="showFileModal = true">附件资料({{ fileList.length }})</el-button>
    <file-uploader v-model="fileList" :show-modal.sync="showFileModal" :disabled="disableForm" :can-download="optType==='detail'" />

  </el-form>
</template>

<script>
import { addMaterialRequisitionFormInfo, updateMaterialRequisitionFormInfo, getMaterialRequisitionFormInfoDetail } from '@/api/materialManage'
// import { getAreaList } from '@/api/common'
import { workflowStatus } from '@/utils/dicData'
import fileUploader from '@/components/FileUploader'
// import TreeModal from '@/components/TreeModal'
import {
  getMaterialDirectoryTree,
  getMaterialDetailList

} from '@/api/basicCode'
export default {
  name: 'AddMaterialRequisitionFormInfo',
  components: {
    fileUploader
    // TreeModal
  },
  props: {

    optType: {
      type: String,
      default: 'detail'
    },

    data: {
      type: Object,
      default() {
        return {}
      }
    }
  },
  data() {
    return {
      getMaterialDirectoryTree,
      getMaterialDetailList,
      showFileModal: false,
      selectRow: null,
      targetList: [],
      workflowStatus,

      addType: '', // same   under
      visible: false,
      showAdd: false,
      selectedNode: null,
      form: {
        issueNumber: '',
        recvTime: '',
        storehouseName: '',
        period: '',
        sender: '',
        stHead: '',
        supplierNumber: '',
        supplierName: '',
        description: '',
        addWorkflowId: '',
        status: '',
        founderName: '',
        createTime: '',
        founderUnit: ''
      },
      columns: [
        {
          label: '材料编号',
          prop: 'number'
        },
        {
          label: '材料名称',
          prop: 'name'
        },
        {
          label: '规格',
          prop: 'specification'
        },
        {
          label: '单位',
          prop: 'unit'
        },
        {
          label: '数量',
          prop: 'count'
        },
        // {
        //   label: '结算节点',
        //   prop: 'settleNode'
        // },
        {
          label: '描述',
          prop: 'description'
        },
        {
          label: '备注',
          prop: 'remark'
        },
        {
          label: '操作',
          prop: 'options',
          slot: 'options'
        }
      ],
      showTargetSetListModal: false,
      fileList: [
      ],
      rules: {
        name: [
          { required: true, message: '单位名称', trigger: 'change' }
        ],
        unifiedSocialCreditCode: [
          { required: true, message: '统一社会信用代码', trigger: 'change' }
        ]
      },
      provinceData: [],
      cityData: []
    }
  },
  computed: {
    disableForm() {
      return this.optType === 'detail'
    }
  },
  watch: {
    data: {
      handler(newData, oldData) {
        if (newData.id) {
          this.getDocDetail(newData.businessId)
        }
      },
      deep: true,
      immediate: true
    }
  },
  mounted() {

  },
  methods: {
    deleteTarget(obj) {
      this.targetList.map((item, m) => {
        if (item.code === obj.code) {
          this.targetList.splice(m, 1)
        }
      })
    },
    getSelectedRows(data) {
      this.targetList = this.targetList.concat(data.map(item => {
        return {
          ...item
        }
      }))
    },
    getDocDetail(id) {
      getMaterialRequisitionFormInfoDetail({ consumeReceiptInfoId: id }).then(res => {
        const { lstAttachment, consumeReceiptInfo, lstConsumeReceiptDetail } = res.data || {}
        if (lstAttachment && lstAttachment.length > 0) {
          this.fileList = lstAttachment
        }
        Object.keys(this.form).forEach(key => {
          const source = consumeReceiptInfo[key]
          if (typeof source === 'boolean') {
            this.form[key] = source ? 1 : 0
          } else {
            this.form[key] = source
          }
        })
        if (lstConsumeReceiptDetail && lstConsumeReceiptDetail.length > 0) {
          this.targetList = lstConsumeReceiptDetail
        }
      })
    },
    deleteDetail() {
    },
    saveDoc() {
      if (this.optType === 'detai') {
        this.show = false
        return
      }
      this.$refs.form.validate((valid) => {
        if (valid) {
          this.targetList.map(item => {
            item.id = ''
            item.name = item.code
          })
          const params = {
            materialRequisitionFormInfo: {
              ...this.form,
              ...this.optType === 'edit' ? {
                id: this.data.id
              } : {}
            },
            lstAttachment: this.fileList,
            lstMaterialRequisitionFormDetail: this.targetList
          }
          const fn = this.optType === 'add' ? addMaterialRequisitionFormInfo : updateMaterialRequisitionFormInfo
          fn(params).then(res => {
            if (res.code === '0001') {
              this.$message({
                type: 'success',
                message: '保存成功!'
              })
              this.show = false
              this.$emit('freshList')
              this.$refs.form.resetFields()
            } else {
              this.$message({
                type: 'error',
                message: res.msg
              })
            }
          })
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.com-form-title{
  font-size: 14px;
  font-weight: bold;
  line-height: 35px;
  color: #409EFF;
  border-bottom: 1px solid #dedede;
  margin-bottom: 6px;
}

::v-deep .el-form-item__label{
  color: #000;
}
::v-deep .el-input.is-disabled .el-input__inner{
  color: #000;
}
::v-deep .el-input.is-disabled .el-input__inner{
  color: #000;

}
</style>
